package coolingPoolDevice

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"xkginweb/commons/response"
	resp "xkginweb/model/entity/comms/response"
	"xkginweb/model/entity/coolingPoolDevice"
	"xkginweb/model/entity/coolingPoolDevice/request"
)

// 定义api接口
type CoolingPoolDeviceApi struct{}

func (api *CoolingPoolDeviceApi) CreateCoolingPoolDevice(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var coolingPoolDevice coolingPoolDevice.QccCoolingPoolDevice
	err := c.ShouldBindJSON(&coolingPoolDevice)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 创建实例，保存冷池检查项
	err = CoolingPoolDeviceService.CreateCoolingPoolDevice(&coolingPoolDevice)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("创建失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("创建成功", c)
}

func (api *CoolingPoolDeviceApi) UpdateCoolingPoolDevice(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var coolingPoolDevice coolingPoolDevice.QccCoolingPoolDevice
	err := c.ShouldBindJSON(&coolingPoolDevice)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = CoolingPoolDeviceService.UpdateCoolingPoolDevice(&coolingPoolDevice)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("更新失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("更新成功", c)
}

// 根据ID查询冷池检查项明细

func (api *CoolingPoolDeviceApi) GetCoolingPoolDevice(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 开始执行
	parseUint, _ := strconv.ParseUint(id, 10, 64)

	data, err := CoolingPoolDeviceService.GetCoolingPoolDevice(uint(parseUint))
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

	response.Ok(data, c)
}

// http://localhost:8888/coolingPoolDevice/delete/:id

func (api *CoolingPoolDeviceApi) DeleteById(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 开始执行
	parseUint, _ := strconv.ParseUint(id, 10, 64)
	err := CoolingPoolDeviceService.DeleteCoolingPoolDeviceById(uint(parseUint))
	if err != nil {
		response.FailWithMessage("删除失败", c)
		return
	}
	response.Ok("ok", c)
}

func (api *CoolingPoolDeviceApi) GetCoolingPoolDeviceDetail(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 这个是用来获取?age=123
	//age := c.Query("age")
	parseUint, _ := strconv.ParseUint(id, 10, 64)
	data, err := CoolingPoolDeviceService.GetCoolingPoolDevice(uint(parseUint))
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	response.Ok(data, c)
}

func (api *CoolingPoolDeviceApi) LoadCoolingPoolDevicePage(c *gin.Context) {
	// 创建一个分页对象
	var pageInfo request.CoolingPoolDevicePageInfo
	// 把前端json的参数传入给PageInfo
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	coolingPoolDevicePage, total, err := CoolingPoolDeviceService.LoadCoolingPoolDevicePage(pageInfo)
	if err != nil {
		response.FailWithMessage("获取失败"+err.Error(), c)
		return
	}
	response.Ok(resp.PageResult{
		List:     coolingPoolDevicePage,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, c)
}

// 添加带房间信息和冷池信息的检查项

func (api *CoolingPoolDeviceApi) CreateDeviceWithPool(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var request request.BindPoolDevice

	err := c.ShouldBindJSON(&request)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}
	var coolingPoolDevice coolingPoolDevice.QccCoolingPoolDevice
	coolingPoolDevice.CoolingPoolId = request.CoolingPoolId
	coolingPoolDevice.DeviceName = request.DeviceName
	//coolingPoolDevice.RoomId = request.RoomId
	// 获取房间信息
	//room, err := RoomService.GetRoom(request.RoomId)
	//if err != nil {
	//	response.FailWithMessage("房间不存在", c)
	//	return
	//}
	//coolingPoolDevice.RoomName = room.RoomName
	//coolingPoolDevice.RoomType = room.RoomType
	// 获取冷池信息
	pool, err := CoolingPoolService.GetCoolingPool(request.CoolingPoolId)
	if err != nil {
		response.FailWithMessage("冷池不存在", c)
		return
	}
	//coolingPoolDevice.CoolingPoolName = pool.CoolingPoolName
	coolingPoolDevice.RoomId = pool.RoomId
	//coolingPoolDevice.RoomName = pool.RoomName
	//coolingPoolDevice.RoomType = pool.RoomType
	// 创建实例，保存冷池检查项
	err = CoolingPoolDeviceService.CreateCoolingPoolDevice(&coolingPoolDevice)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("创建失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("创建成功", c)
}
